<template>
  <div>
    <NavHeader @add="add" />
    <NavMain :list="list" @del="del" />
    <NavFooter :list="list" @clear="clearTodo" />
  </div>
</template>

<script>
import { computed, defineComponent, ref } from 'vue'
import NavHeader from '@/components/navHeader/NavHeader'
import NavMain from '@/components/navMain/NavMain'
import NavFooter from '@/components/navFooter/NavFooter'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'


export default defineComponent({
  name: 'HomeView',
  components: {
    NavHeader,
    NavMain,
    NavFooter
  },
  setup() {
    let store = useStore()
    let list = computed(() => {
      return store.state.list
    })
    let value = ref('')
    // 添加任务
    let add = (val) => {
      value.value = val
      // 先判断有没有重复的任务 如果任务存在 不能重复添加
      let flag = true
      list.value.map((item) => {
        if (item.title === value.value) {
          // 有重复的任务
          flag = false
          alert('任务已存在')
        }
      })
      // 如果没有重复的任务
      if (flag) {
        // 调用mutations中的add方法
        store.commit('addTodo', {
          title: value.value,
          completed: false
        })
      }
    }
    // 删除任务
    let del = (val) => {
      // 调用mutations中的删除方法 
      store.commit('delTodo', val)
    }
    // 清除已完成的任务
    let clearTodo = (val) => {
      // 调用mutations中的clear方法
      store.commit('clear', val)
    }
    return {
      add,
      value,
      list,
      clearTodo,
      del
    }
  }
})
</script>

<style scoped lang="scss"></style>